package org.compiere.model;

import java.io.File;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;

import org.apache.poi.hslf.model.Line;
import org.compiere.process.DocAction;
import org.compiere.process.DocumentEngine;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class MBCSACRInputFormCompany extends X_BCS_ACRInputForm_Company implements DocAction{

	private static CLogger s_log = CLogger.getCLogger(MBCSACRInputFormCompany.class);
	
	public MBCSACRInputFormCompany(Properties ctx, int MBCSACRInputFormCompany_ID,
			String trxName) {
		super(ctx, MBCSACRInputFormCompany_ID, trxName);
		// TODO Auto-generated constructor stub
	}
	
	public MBCSACRInputFormCompany(Properties ctx, ResultSet rs, String trxName){
		super(ctx, rs, trxName);
	}

	protected boolean afterSave(boolean newRecord, boolean success){
		
		boolean is = false;
		
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";	

		//create appendix1
//		sql = " SELECT * FROM BCS_ACRInputFormAppend1 \n " +
//				   " WHERE BCS_ACRInputForm_Period_ID ="  +  get_ID();
//
//			try{
//				pstmt = DB.prepareStatement(sql,null);				
//				rs = pstmt.executeQuery();	
//				if(rs.next()){
//					is = true;
//				}
//			}catch (SQLException e){
//				s_log.log(Level.SEVERE, "MBCSACRInputFormPeriod", e);
//			}finally{
//				DB.close(rs, pstmt);
//				rs = null; pstmt = null;
//			}			
//
//		if(is);	//if there are record then do nothing
//		else{	//else create position from setupin
//
//			MBCSACRInputFormAppend1 line = new MBCSACRInputFormAppend1(getCtx(), 0, get_TrxName());
//			line.setBCS_ACRInputForm_Period_ID(getBCS_ACRInputForm_Period_ID());
//			line.save(get_TrxName());
//					
//		}
		//create appendix1
		is = false;
		sql = " SELECT * FROM BCS_ACRInputFormAppend1 \n " +
				   " WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID();

			try{
				pstmt = DB.prepareStatement(sql,null);				
				rs = pstmt.executeQuery();	
				if(rs.next()){
					is = true;
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormCompany", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			

		if(is);	//if there are record then do nothing
		else{	//else copy last copy
			int bpartner_id = getC_BPartner_ID();
			
			sql = 
					" select max(a1.bcs_acrinputformappend1_id) lastID \n" +
							" from adempiere.bcs_acrinputformappend1 a1\n" +
							" inner join adempiere.bcs_acrinputform_company com on  com.bcs_acrinputform_company_id = a1.bcs_acrinputform_company_id\n" +
							" where com.c_bpartner_id = "		
			+ bpartner_id;
			try{
				String Trx = get_TrxName();
				pstmt = DB.prepareStatement(sql,get_TrxName());				
				rs = pstmt.executeQuery();	
				while(rs.next()){
					Integer i = rs.getInt(1);
					if(i != null && i > 0){
						MBCSACRInputFormAppend1 orig = new MBCSACRInputFormAppend1(getCtx(), rs.getInt(1), get_TrxName());
						MBCSACRInputFormAppend1 copy = new MBCSACRInputFormAppend1(Env.getCtx(), 0, get_TrxName());
						
						orig.copyValues(orig, copy);
						copy.setBCS_ACRInputForm_Company_ID(get_ID());
						copy.save(get_TrxName());
						

					}
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormCompany", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			
		}		
		
		
		
		//create appendix2
		is = false;
		sql = " SELECT * FROM BCS_ACRInputFormAppend2 \n " +
				   " WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID();

			try{
				pstmt = DB.prepareStatement(sql,null);				
				rs = pstmt.executeQuery();	
				if(rs.next()){
					is = true;
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormPeriod", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			

		if(is);	//if there are record then do nothing
		else{	//else create position from setupin

			sql = " SELECT BCS_ACRPositionsDict_ID FROM BCS_ACRSetupInFormAppend2 order by BCS_ACRSetupInFormAppend2_ID";
			try{
				String Trx = get_TrxName();
				pstmt = DB.prepareStatement(sql,get_TrxName());				
				rs = pstmt.executeQuery();	
				while(rs.next()){
					MBCSACRInputFormAppend2 line = new MBCSACRInputFormAppend2(getCtx(), 0, get_TrxName());
					line.setBCS_ACRPositionsDict_ID(rs.getInt(1));
					line.setBCS_ACRInputForm_Company_ID(getBCS_ACRInputForm_Company_ID());
					line.setAD_Org_ID(getAD_Org_ID());
					line.save(get_TrxName());
					
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormPeriod", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			
		}

		
		//create appendix3_1
		is = false;
		sql = " SELECT * FROM BCS_ACRInputFormAppend3_1 \n " +
				   " WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID();

			try{
				pstmt = DB.prepareStatement(sql,null);				
				rs = pstmt.executeQuery();	
				if(rs.next()){
					is = true;
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormPeriod", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			

		if(is);	//if there are record then do nothing
		else{	//else create position from setupin

			sql = " SELECT BCS_ACRPositionsDict_ID, C_UOM_ID FROM BCS_ACRSetupInFormAppend3_1 order by BCS_ACRSetupInFormAppend3_1_ID";
			try{
				String Trx = get_TrxName();
				pstmt = DB.prepareStatement(sql,get_TrxName());				
				rs = pstmt.executeQuery();	
				while(rs.next()){
					MBCSACRInputFormAppend3_1 line = new MBCSACRInputFormAppend3_1(getCtx(), 0, get_TrxName());
					line.setBCS_ACRPositionsDict_ID(rs.getInt(1));
					line.setBCS_ACRInputForm_Company_ID(getBCS_ACRInputForm_Company_ID());
					line.setC_UOM_ID(rs.getInt(2));
					line.setAD_Org_ID(getAD_Org_ID());
					line.save(get_TrxName());
					
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormPeriod", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			
		}
		

		//create appendix3_2
		is = false;
		sql = " SELECT * FROM BCS_ACRInputFormAppend3_2 \n " +
				   " WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID();

			try{
				pstmt = DB.prepareStatement(sql,null);				
				rs = pstmt.executeQuery();	
				if(rs.next()){
					is = true;
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormPeriod", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			

		if(is);	//if there are record then do nothing
		else{	//else create position from setupin

			sql = " SELECT BCS_ACRPositionsDict_ID, C_UOM_ID FROM BCS_ACRSetupInFormAppend3_2 order by BCS_ACRSetupInFormAppend3_2_ID";
			try{
				String Trx = get_TrxName();
				pstmt = DB.prepareStatement(sql,get_TrxName());				
				rs = pstmt.executeQuery();	
				while(rs.next()){
					MBCSACRInputFormAppend3_2 line = new MBCSACRInputFormAppend3_2(getCtx(), 0, get_TrxName());
					line.setBCS_ACRPositionsDict_ID(rs.getInt(1));
					line.setBCS_ACRInputForm_Company_ID(getBCS_ACRInputForm_Company_ID());
					line.setC_UOM_ID(rs.getInt(2));
					line.setAD_Org_ID(getAD_Org_ID());
					line.save(get_TrxName());
					
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormPeriod", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			
		}
		
		//create appendix3_3
		is = false;
		sql = " SELECT * FROM BCS_ACRInputFormAppend3_3 \n " +
				   " WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID();

			try{
				pstmt = DB.prepareStatement(sql,null);				
				rs = pstmt.executeQuery();	
				if(rs.next()){
					is = true;
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormPeriod", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			

		if(is);	//if there are record then do nothing
		else{	//else create position from setupin

			sql = " SELECT BCS_ACRPositionsDict_ID FROM BCS_ACRSetupInFormAppend3_3 order by BCS_ACRSetupInFormAppend3_3_ID ";
			try{
				String Trx = get_TrxName();
				pstmt = DB.prepareStatement(sql,get_TrxName());				
				rs = pstmt.executeQuery();	
				while(rs.next()){
					MBCSACRInputFormAppend3_3 line = new MBCSACRInputFormAppend3_3(getCtx(), 0, get_TrxName());
					line.setBCS_ACRPositionsDict_ID(rs.getInt(1));
					line.setBCS_ACRInputForm_Company_ID(getBCS_ACRInputForm_Company_ID());
					line.setAD_Org_ID(getAD_Org_ID());
//					line.setC_UOM_ID(rs.getInt(2));
					line.save(get_TrxName());
					
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormPeriod", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			
		}
		
		
		//create vgoo companies
		is = false;
		sql = " SELECT * FROM BCS_ACRInputForm_IntCompany where BCS_ACRInputForm_Company_ID = "  +  get_ID();

			try{
				pstmt = DB.prepareStatement(sql,null);				
				rs = pstmt.executeQuery();	
				if(rs.next()){
					is = true;
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormCompany", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			

		if(is);	//if there are record then do nothing
		else{
			List<Integer> list = new ArrayList<Integer>();
	
			sql = 	" select c_bpartner_id \n"	+
					"	from adempiere.BCS_CompPerimeterDict \n " +
					" 	order by c_bpartner_id	";
	
				try{
					pstmt = DB.prepareStatement(sql,null);				
					rs = pstmt.executeQuery();	
					while(rs.next()){
						list.add(rs.getInt(1));
					}
				}catch (SQLException e){
					s_log.log(Level.SEVERE, "MBCSACRInputFormCompany", e);
				}finally{
					DB.close(rs, pstmt);
					rs = null; pstmt = null;
				}			
	
				for(Integer i: list){
					MBCSACRInputFormIntCompany company = new MBCSACRInputFormIntCompany(Env.getCtx(), 0, null);
					company.setBCS_ACRInputForm_Company_ID(get_ID());
					company.setC_BPartner_ID(i);
//					company.setAD_Org_ID(getAD_Org_ID());
					switch(i){
						case 1000084:
							company.setAD_Org_ID(1000000);
							break;
						case 1000022:
							company.setAD_Org_ID(1000001);
							break;
						case 1000021:
							company.setAD_Org_ID(1000002);
							break;
						case 1000018:
							company.setAD_Org_ID(1000003);
							break;
						case 1000020:
							company.setAD_Org_ID(1000006);
							break;
						case 1000019:
							company.setAD_Org_ID(1000004);
							break;
						case 2000086:
							company.setAD_Org_ID(1000005);
							break;
					}

					company.save();
				
				}	
			}
		
	return true;
}	

	protected boolean afterDelete(boolean success){
		
//		delete all child item
				
				DB.executeUpdate("delete BCS_ACRInputFormAppend1 WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID());
				DB.executeUpdate("delete BCS_ACRInputFormAppend2 WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID());
				DB.executeUpdate("delete BCS_ACRInputFormAppend3_1 WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID());
				DB.executeUpdate("delete BCS_ACRInputFormAppend3_2 WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID());
				DB.executeUpdate("delete BCS_ACRInputFormAppend3_3 WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID());
				DB.executeUpdate("delete BCS_ACRInputFormAppend4 WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID());
				DB.executeUpdate("delete bcs_acrinputform_intcompany WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID());
				
		
		return true;
		
	}

	@Override
	public boolean processIt(String processAction) throws Exception {
//		// TODO Auto-generated method stub
//		return false;
		m_processMsg = null;
		DocumentEngine engine = new DocumentEngine (this, getDocStatus());
		return engine.processIt (processAction, getDocAction());		
	}

	/**	Process Message 			*/
	private String		m_processMsg = null;
	/**	Just Prepared Flag			*/
	private boolean		m_justPrepared = false;	
	
	@Override
	public boolean unlockIt() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean invalidateIt() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String prepareIt() {
		
		System.out.println("prepareIT()");
		
		log.info(toString());
//		m_processMsg = ModelValidationEngine.get().fireDocValidate(this, ModelValidator.TIMING_BEFORE_COMPLETE);
//		if (m_processMsg != null)
//			return DocAction.STATUS_Invalid;
//
//		m_processMsg = ModelValidationEngine.get().fireDocValidate(this, ModelValidator.TIMING_AFTER_COMPLETE);
//		if (m_processMsg != null)
//			return DocAction.STATUS_Invalid;

		
		//	Add up Amounts
		// a.nurpiissov
		String docStatus  = getDocStatus();
		if(docStatus.equals(DocAction.STATUS_Drafted))
			return DocAction.STATUS_Approved;
		
//		m_justPrepared = true;
		if(docStatus.equals(DocAction.STATUS_Approved))
//		if (!DOCACTION_Complete.equals(getDocStatus()))
//			setDocAction(DOCACTION_Complete);
		return DocAction.STATUS_Completed;
		
		
		return null;
	}

	@Override
	public boolean approveIt() {
		System.out.println("approveIT()");
////		return false;
////		setIsApproved(true);
////		return true;
////		String s = getDocStatus();
////		if(s.equals(DocAction.ACTION_Approve))
////			setDocAction(DocAction.ACTION_Complete);
//		setIsApproved(true);
//		setDocAction(DocAction.ACTION_Checked);
//		return true;//DocAction.STATUS_Approved;
		
		log.info(toString());
//		setIsApproved(true);
		return true;
	}

//	private void setIsApproved(boolean b) {
//		set_ValueNoCheck (COLUMNNAME_IsApproved, Boolean.valueOf(IsApproved));
//	}

	@Override
	public boolean rejectIt() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String completeIt() {
		System.out.println("comleteIT()");
//		return null;
//		String s = getDocStatus();
//		if(s.equals(DocAction.STATUS_Completed)){
//			setProcessed(true);
//			setDocAction(DocAction.ACTION_Commited);
			return DocAction.STATUS_Commited;
//		}
		
//		return DocAction.STATUS_Approved;
	}

	@Override
	public boolean voidIt() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean closeIt() {
//		setIsClosed(true);
//		setProcessed(true);
//		setDocAction(DocAction.ACTION_Close);
//		setDocStatus(DocAction.STATUS_Closed);
		return true;
	}

	@Override
	public boolean reverseCorrectIt() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean reverseAccrualIt() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean reActivateIt() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String getSummary() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getDocumentNo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getDocumentInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public File createPDF() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getProcessMsg() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getDoc_User_ID() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getC_Currency_ID() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public BigDecimal getApprovalAmt() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public String getDocStatus() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getDocAction() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
